﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using com.ubosi.spider.core.Net;
using com.ubosi.spider.hotel.HotelComment;
using com.ubosi.model.Entity;

namespace com.ubosi.spider.app.Hotel
{
    public partial class frmHotelComment : Form
    {
        public frmHotelComment()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 比较日期，前者大于后者返回1,等于返回0，小于返回-1
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dest"></param>
        /// <returns></returns>
        private int CompareDate(DateTime src, DateTime dest)
        {
            if (DateTime.Parse(src.ToString("yyyy-MM-dd")) > DateTime.Parse(dest.ToString("yyyy-MM-dd")))
            {
                return 1;
            }
            else if (DateTime.Parse(src.ToString("yyyy-MM-dd")) == DateTime.Parse(dest.ToString("yyyy-MM-dd")))
            {
                return 0;
            }
            else
            {
                return -1;
            }
        }

        private void StartSpider()
        {
            if (cbSrcId.SelectedItem == null)
            {
                MessageBox.Show("请选择来源");
                return;
            }

            if (CompareDate(dtpStartDate.Value,DateTime.Now)==1)
            {
                MessageBox.Show("开始日期不能大于今天日期！");
                return;
            }

            if (CompareDate(dtpStartDate.Value,dtpEndDate.Value)==1)
            {
                MessageBox.Show("开始日期不能大于今天日期！");
                return;
            }

            NetSpider netSpider = new NetSpider(SpiderGlobal.RequestSetting, null);

            Spider17UHotelComment spider17UHotelComment = new Spider17UHotelComment(netSpider, SpiderGlobal.SpiderConfig);
            
            string srcId = cbSrcId.SelectedItem.ToString();
            UBOSIEntities dataContext = new UBOSIEntities();
            var query = from hotelMap in dataContext.HotelMap
                        where hotelMap.SrcId == srcId
                        select hotelMap;
            
            int count = query.Count();
            pbSpiderProgress.Maximum = count;
            pbSpiderProgress.Minimum = 0;
            pbSpiderProgress.Style = ProgressBarStyle.Blocks;

            foreach (var map in query)
            {
                if (pbSpiderProgress.Value < pbSpiderProgress.Maximum)
                {
                    pbSpiderProgress.Value++;
                    pbSpiderProgress.Update();
                }

                var queryComment = from comment in dataContext.HotelComment
                                   where comment.HotelId == map.HotelId
                                   //orderby comment.CommentDate descending
                                   select comment;
                HotelComment lastComment = null;
                try
                {
                    foreach (var hc in queryComment)
                    {
                        lastComment = hc;
                        break;
                    }
                }
                catch
                {
                    continue;
                }

                DateTime startTime = dtpStartDate.Value;
                DateTime endTime = dtpEndDate.Value;

                if (lastComment != null)
                {
                    continue;
                }
                //if (lastComment != null && lastComment.CommentDate != null)
                //{
                //    if (endTime <= lastComment.CommentDate)
                //    {
                //        continue;
                //    }
                //    else
                //    {
                //        startTime = ((DateTime)lastComment.CommentDate).AddDays(1);
                //    }
                //}

                List<HotelComment> list = spider17UHotelComment.SpiderHotelComment(map.SrcHotelId.Trim(),startTime, endTime);
                System.Threading.Thread.Sleep(50);
                foreach (HotelComment hotelComment in list)
                {
                    hotelComment.HotelId = map.HotelId.Trim();
                    SaveHotelComment(hotelComment);
                }
                
            }

        }

        

        private void SaveHotelComment(HotelComment hotelComment)
        {
            UBOSIEntities dataContext = new UBOSIEntities();
            dataContext.AddToHotelComment(hotelComment);
            dataContext.SaveChanges();
        }

        private void btnCrawTest_Click(object sender, EventArgs e)
        {
            NetSpider netSpider = new NetSpider(SpiderGlobal.RequestSetting, null);

            Spider17UHotelComment spider17UHotelComment = new Spider17UHotelComment(netSpider, SpiderGlobal.SpiderConfig);
            //注意正式运行需要注释下面的代码
            spider17UHotelComment.LocalFileName = @"C:\工作目录\游博网\SourceCode\UBOSI\Spider\com.ubosi.spider.hotel\HotelComment\17U_HotelComment.htm";
            spider17UHotelComment.SpiderHotelComment("123456", DateTime.Now, DateTime.Now);
            
        }

        /// <summary>
        /// 抓取按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSpider_Click(object sender, EventArgs e)
        {
            StartSpider();
        }
    }
}
